const router = require('koa-router')()

const allSqlAction = require("../lib/mysql")

router.get('/', async (ctx, next) => {
  await ctx.render('index', {
    title: 'Hello Koa 2!' 
  })
})

router.get('/string', async (ctx, next) => {
  ctx.body = 'koa2 string'
})

router.get('/json', async (ctx, next) => {
  ctx.body = {
    title: 'koa2 json',
    status: ctx.status
  }
})
router.post('/mysql', async (ctx, next) => {
  // let id = ctx.request.body
  let {id,id2} = ctx.request.body

  res = checkUser(id)

  ctx.body = {
    id: id,
    title: 'koa2 json',
    status: ctx.status,
    res: res
  }
})

// router.get('/mysql', async (ctx, next) => {
// let sql = `select * from pets`
// allSqlAction.allSqlAction(sql).then(res => {

//   ctx.response.body = 1

// })
// })


async function checkUser(id) {
  let sql = `select * from pets where id = ${id}`
  return allSqlAction.allSqlAction(sql).then(res => {
    console.log(res)

    // return res
    return "123"
  })
}

module.exports = router
